package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.entity.Category;
import com.sky.result.Result;
import com.sky.service.AddressService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/user/addressBook")
public class AddressController {
    private final AddressService addressService;

    @GetMapping("/list")
    public Result<List<AddressBook>> selectAddress() {
        List<AddressBook> addressBooks = addressService.selectAddress();
        return Result.success(addressBooks);
    }

    @GetMapping("/default")
    public Result<AddressBook> selectDefaultAddress() {
        AddressBook addressBook = addressService.selectDefaultAddress();
        return Result.success(addressBook);
    }

    @GetMapping("/{id}")
    public Result<AddressBook> selectAddressById(@PathVariable String id) {
        AddressBook addressBook = addressService.selectAddressById(id);
        return Result.success(addressBook);
    }

    @PostMapping
    public Result<?> addAddress(@RequestBody AddressBook addressBook) {
        addressService.addAddress(addressBook);
        return Result.success();
    }

    @PutMapping
    public Result<?> updateAddress(@RequestBody AddressBook addressBook) {
        addressService.updateAddress(addressBook);
        return Result.success();
    }

    @PutMapping("/default")
    public Result<?> updateDefaultAddress(@RequestBody AddressBook addressBook) {
        addressService.updateDefaultAddress(addressBook);
        return Result.success();
    }

    @DeleteMapping
    public Result<?> deleteAddressById(String id) {
        addressService.deleteAddressById(id);
        return Result.success();
    }
}
